package untiring.controllers;

import java.net.URLEncoder;





import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import untiring.interfaces.MemberService;
import untiring.interfaces.ZipcodeService;
import untiring.models.MemberVO;

@Controller
@RequestMapping("/join/*")
public class JoinContrioller {
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private ZipcodeService zipcodeservice;
	
	
	@RequestMapping(value="/agree", method = RequestMethod.GET)
	public String agree() throws Exception{
		return "join/agree";
	}
	
	@RequestMapping(value="/realcheck", method = RequestMethod.GET)
	public String realcheckget() throws Exception{
		return "join/realcheck";
	}
	
	@RequestMapping(value="/realcheck", method = RequestMethod.POST)
	public String realcheckpost(@RequestParam String name2, @RequestParam String jumin1, @RequestParam String jumin2) throws Exception{
		String viewPage =  "join/realcheck";
		name2 = URLEncoder.encode(name2,"UTF-8");
		// 가입자의 이름은URL을 이용해서 get 방식으로 서버에 전송하므로
		// 반드시 UTF-8로 encode해야 함
		// 또한, server.xml에 64번째 줄에 URIEncoding="UTF-8"구문도 작성해야 함
		StringBuilder url = new StringBuilder();
		url.append("?name2=").append(name2).append("&jumin1=").append(jumin1).append("&jumin2=").append(jumin2);
		viewPage = "redirect:/join/joininfo.mvc"+url.toString();
		return viewPage;
	}
	
	@RequestMapping(value="/joininfo", method = RequestMethod.GET)
	public String joininfoget() throws Exception{
		return "join/joininfo";
	}
	
	@RequestMapping(value="/joininfo", method = RequestMethod.POST)
	public String joininfopost(@ModelAttribute MemberVO m) throws Exception{
		
		return memberService.newMember(m);
	}

	@RequestMapping(value="/joinok", method = RequestMethod.GET)
	public String joinok() throws Exception{
		return "join/joinok";
	}
	
	/*@RequestMapping(value="/join/zipcode", method = RequestMethod.GET)
	public String zipcodepost() throws Exception{
		return "join/zipcode";
	}*/
	
	/*@RequestMapping(value="/join/zipcode", method = RequestMethod.POST)
	public String zipcodeget(Model model,@RequestParam String dong) throws Exception{
		model.addAttribute("zipcodelist", zipcodeservice.readZipcode(dong) );
		return "join/zipcode";
	}*/
	@RequestMapping(value="/join/zipcode", method = RequestMethod.GET , produces="text/plain;charset=UTF-8")
	@ResponseBody
	public String zipcodeget(@RequestParam String dong) throws Exception{
		
		return zipcodeservice.readZipcode(dong);
	}

	@RequestMapping(value="/join/idchek", method = RequestMethod.GET)
	@ResponseBody
	public String idcheck(@RequestParam String myuid) throws Exception{
		if(memberService.idcheck(myuid))
			return "y";
		else
			return "n";
	}

}
